'use strict';
const crypto = require('crypto');
const { format } = require('date-fns');
module.exports = app => {
    const { STRING, INTEGER, DATE, ENUM, TEXT } = app.Sequelize;
    // 配置（重要：一定要配置详细，一定要！！！）
    const Courses = app.model.define('courses',{
        id: {
            type: INTEGER(20).UNSIGNED,
            primaryKey: true,
            autoIncrement: true
        },
        coursesname: {
            type: STRING(200),
            allowNull: false,
            defaultValue: '',
            comment: '课程名称',
        },
        originalprice: {
            type: INTEGER,
            allowNull: false,
            defaultValue: 0,
            comment: '课程价格',
        },
        sellingprice: {
            type: INTEGER,
            allowNull: false,
            defaultValue: 0,
            comment: '课程售价',
        },
        coursessellstatus: {
            type: INTEGER(1),
            allowNull: false,
            defaultValue: 1,
            comment: '上架 0禁用1启用' 
        },
        coursescoverimg: {
            type: STRING(260),
            comment: '主图',
        },
        coursestype: {
            type: INTEGER(1),
            allowNull: false,
            comment: '课程类型 1个人成长 2亲密关系 3亲子关系 4情绪管理' 
        },
        coursesimg: {
            type: STRING(260),
            comment: '副图',
        },
        coursesrecommend: {
            type: INTEGER(1),
            allowNull: false,
            defaultValue: 1,
            comment: '推荐 0禁用1启用' 
        },
        courseslecturer: {
            type: STRING(200),
            allowNull: false,
            defaultValue: '',
            comment: '课程老师' 
        },
        coursesdetailcontent: {
            type: STRING(10000),
            allowNull: true,
            defaultValue: '',
            comment: '课程详细' 
        },
        created_at: {
            type : DATE,
            get() {
                const rawValue = this.getDataValue('created_at');
                return rawValue ? format(new Date(rawValue), 'yyyy-MM-dd HH:mm:ss') : null;
            }
        },
        updated_at: DATE
    });

    // // 定义关联关系
    // User.associate = function (model) {
    //     User.hasMany(app.model.Friend, {
    //         as: "bfriends", // 当前用户的被好友
    //         foreignKey: 'friend_id'
    //     });

    //     User.hasMany(app.model.Friend, {
    //         as: "friends", // 当前用户的好友
    //         foreignKey: 'user_id'
    //     });

    //     User.hasMany(app.model.Moment, {
    //         foreignKey: 'user_id'
    //     });
    // }
     // 定义关联关系
     Courses.associate = function (model) {
        // 一对多
        Courses.hasMany(app.model.Video);
    }

    return Courses;
};